package at.fhooe.mcm.logic.core;

import java.util.Random;
import java.util.UUID;

public class NameService implements Service
{

	public static final String	NAME		= "core.nameservice";

	private boolean				m_isGlobal	= false;
	private int					m_counter	= 0;
	private Random				m_random	= new Random();

	public static NameService getInstance(Core _core)
	{
		return (NameService) _core.getService(NAME);
	}

	@Override
	public String getName()
	{
		return NAME;
	}

	@Override
	public void initialize(Core _core)
	{

	}

	public String generateName()
	{
		if (m_isGlobal)
		{
			return generateGlobalName();
		}
		else
		{
			return generateLocalName();
		}
	}

	private String generateGlobalName()
	{
		return UUID.randomUUID().toString();
	}

	private String generateLocalName()
	{
		return Integer.toHexString(Math.abs(m_random.nextInt())) + "-" + Integer.toHexString(++m_counter);
	}

	public void setGlobalMode(boolean _state)
	{
		m_isGlobal = _state;
	}

	public boolean isGlobal()
	{
		return m_isGlobal;
	}

	@Override
	public void shutdown(Core core)
	{
		// TODO Auto-generated method stub
		
	}

}
